// Copyright (c) 2022 NetEase, Inc. All rights reserved.
// Use of this source code is governed by a MIT license that can be
// found in the LICENSE file.

package com.netease.yunxin.app.im.main;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.zhht.htzx.R;
import com.netease.yunxin.app.im.message.ProductMessageUtils;
import com.netease.yunxin.kit.alog.ALog;
import com.netease.yunxin.kit.chatkit.ui.ChatKitClient;

/**
 * 发送商品消息示例Activity
 */
public class SendProductMessageActivity extends AppCompatActivity {

    private static final String TAG = "SendProductMessageActivity";

    private EditText etTargetId;
    private EditText etProductName;
    private EditText etProductPrice;
    private EditText etProductImage;
    private EditText etProductDesc;
    private EditText etProductUrl;
    private Button btnSendProduct;
    private Button btnTestProduct;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_product_message);
        
        ALog.d(TAG, "=== SendProductMessageActivity onCreate ===");
        
        initViews();
        initListeners();
        
        // 验证注册状态
        checkRegistrationStatus();
    }

    private void initViews() {
        etTargetId = findViewById(R.id.et_target_id);
        etProductName = findViewById(R.id.et_product_name);
        etProductPrice = findViewById(R.id.et_product_price);
        etProductImage = findViewById(R.id.et_product_image);
        etProductDesc = findViewById(R.id.et_product_desc);
        etProductUrl = findViewById(R.id.et_product_url);
        btnSendProduct = findViewById(R.id.btn_send_product);
        btnTestProduct = findViewById(R.id.btn_test_product);

        // 设置默认值
        etTargetId.setText("test001");
        etProductName.setText("iPhone 15 Pro");
        etProductPrice.setText("¥7999");
        etProductImage.setText("https://example.com/iphone15.jpg");
        etProductDesc.setText("最新款iPhone，搭载A17 Pro芯片，支持USB-C接口");
        etProductUrl.setText("https://www.apple.com/iphone-15-pro/");
        
        ALog.d(TAG, "Views initialized successfully");
    }

    private void initListeners() {
        btnSendProduct.setOnClickListener(v -> {
            ALog.d(TAG, "发送商品消息按钮被点击");
            sendProductMessage();
        });
        
        btnTestProduct.setOnClickListener(v -> {
            ALog.d(TAG, "发送测试消息按钮被点击");
            sendTestProductMessage();
        });
        
        ALog.d(TAG, "Listeners initialized successfully");
    }
    
    /**
     * 检查注册状态
     */
    private void checkRegistrationStatus() {
        ALog.d(TAG, "=== 检查注册状态 ===");
        ALog.d(TAG, "ChatKitClient: " + (ChatKitClient.getChatUIConfig()!= null ? "已初始化" : "未初始化"));
        
        // 检查布局资源是否存在
        checkLayoutResources();
        
        ALog.d(TAG, "注册状态检查完成");
    }
    
    /**
     * 检查布局资源
     */
    private void checkLayoutResources() {
        ALog.d(TAG, "=== 检查布局资源 ===");
        
        try {
            // 检查布局资源ID
            int itemProductMessageId = R.layout.item_product_message;
            int itemProductMessageSelfId = R.layout.item_product_message_self;
            
            ALog.d(TAG, "item_product_message ID: " + itemProductMessageId);
            ALog.d(TAG, "item_product_message_self ID: " + itemProductMessageSelfId);
            
            // 检查视图组件ID
            int productImageId = R.id.product_image;
            int productNameId = R.id.product_name;
            int productDescId = R.id.product_desc;
            int productPriceId = R.id.product_price;
            
            ALog.d(TAG, "product_image ID: " + productImageId);
            ALog.d(TAG, "product_name ID: " + productNameId);
            ALog.d(TAG, "product_desc ID: " + productDescId);
            ALog.d(TAG, "product_price ID: " + productPriceId);
            
            ALog.d(TAG, "布局资源检查完成");
            
        } catch (Exception e) {
            ALog.e(TAG, "布局资源检查失败: " + e.getMessage());
            e.printStackTrace();
        }
    }

    private void sendProductMessage() {
        String targetId = etTargetId.getText().toString().trim();
        String productName = etProductName.getText().toString().trim();
        String productPrice = etProductPrice.getText().toString().trim();
        String productImage = etProductImage.getText().toString().trim();
        String productDesc = etProductDesc.getText().toString().trim();
        String productUrl = etProductUrl.getText().toString().trim();

        ALog.d(TAG, "=== 发送商品消息 ===");
        ALog.d(TAG, "targetId: " + targetId);
        ALog.d(TAG, "productName: " + productName);
        ALog.d(TAG, "productPrice: " + productPrice);

        // 验证输入
        if (TextUtils.isEmpty(targetId)) {
            Toast.makeText(this, "请输入目标用户ID", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(productName)) {
            Toast.makeText(this, "请输入商品名称", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(productPrice)) {
            Toast.makeText(this, "请输入商品价格", Toast.LENGTH_SHORT).show();
            return;
        }

        try {
            // 发送商品消息
            String productId = String.valueOf(System.currentTimeMillis());
            ProductMessageUtils.sendProductMessage(
                targetId,
                productId,
                productName, 
                productPrice, 
                productImage, 
                productDesc, 
                productUrl
            );
            
            Toast.makeText(this, "商品消息发送成功", Toast.LENGTH_SHORT).show();
            ALog.d(TAG, "Product message sent successfully");
            
        } catch (Exception e) {
            Toast.makeText(this, "发送失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
            ALog.e(TAG, "Send product message failed: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    /**
     * 发送测试商品消息
     */
    private void sendTestProductMessage() {
        String targetId = etTargetId.getText().toString().trim();
        
        ALog.d(TAG, "=== 发送测试商品消息 ===");
        ALog.d(TAG, "targetId: " + targetId);
        
        if (TextUtils.isEmpty(targetId)) {
            Toast.makeText(this, "请输入目标用户ID", Toast.LENGTH_SHORT).show();
            return;
        }
        
        try {
            // 发送测试商品消息
            ProductMessageUtils.sendTestProductMessage(targetId);
            
            Toast.makeText(this, "测试商品消息发送成功", Toast.LENGTH_SHORT).show();
            ALog.d(TAG, "Test product message sent successfully");
            
        } catch (Exception e) {
            Toast.makeText(this, "发送失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
            ALog.e(TAG, "Send test product message failed: " + e.getMessage());
            e.printStackTrace();
        }
    }
} 